﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class BuildManager : MonoBehaviour
{
    public GameObject upgradeUI;
    public Button upgrade_button;
    public Text money_text;
    private int money = 1000;
    public TurretData[] turret_types = new TurretData[3];
    private TurretData selected_turret;
    public static GameObject selected_pre;
    public Animator money_flick;
    public static int cube_id = -1;
    private MapCube selected_cube;
    public static MapCube skim_cube;
    public GameObject BuildEffect;
    public Animator UI_anim;

    private void Update()
    {
        if (Input.GetMouseButtonDown(0) && EventSystem.current.IsPointerOverGameObject() == false)
        {                       //这里EventSystem检测是否为按键、单多选等事件，让这些事件优先
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit, 1000, LayerMask.GetMask("MapCube")))
            {
                selected_cube = hit.collider.gameObject.GetComponent<MapCube>();
                if (selected_turret != null)
                {
                    if (money >= selected_turret.cost)
                    {
                        if (selected_cube.BuildTurret(selected_turret))
                        {
                            GameObject go = GameObject.Instantiate(BuildEffect, selected_cube.transform.position, Quaternion.identity);
                            Destroy(go,0.5f); //建筑特效释放0.5秒
                            Purchase(selected_turret.cost);
                        }
                    }
                    else
                    {
                        money_flick.SetTrigger("flicker");
                    }
                }
                else
                {
                    selected_cube.GetUpgradeUI(upgradeUI, UI_anim, upgrade_button);
                }
            }
        }
        if (Input.GetMouseButtonDown(1)) //右键取消已选
        {
            DenyBuild();
        }
    }

    public void DenyBuild()
    {
        if (selected_turret != null)
        {
            selected_turret = null;
            selected_pre.SetActive(false);
            selected_pre = null;
            if (skim_cube != null)
            {
                skim_cube.cube_renderer.material.color = Color.white;
            }
        }
    }

    public void UpgradeTurret()
    {
        if (money >= selected_cube.builded_type.upgrade_cost)
        {
            upgrade_button.interactable = false; //使升级按钮失效
            selected_cube.UpgradeTurret();
            GameObject go = GameObject.Instantiate(BuildEffect, selected_cube.transform.position, Quaternion.identity);
            Destroy(go, 0.5f); //建筑特效释放0.5秒
            Purchase(selected_cube.builded_type.upgrade_cost);
        }
        else
        {
            money_flick.SetTrigger("flicker");
        }
    }

    public void DestroyTurret()
    {
        upgradeUI.SetActive(false);
        selected_cube.DestroyTurret();
    }

    private void Purchase(int cost) //建造或升级花钱
    {
        money -= cost;
        money_text.text = "$" + money.ToString();
    }

    public void SelectStandard(bool is_on)
    {
        if (is_on)
        {
            OnSelect(0);
        }
    }
    public void SelectMissil(bool is_on)
    {
        if (is_on)
        {
            OnSelect(1);
        }
    }
    public void SelectLaser(bool is_on)
    {
        if (is_on)
        {
            OnSelect(2);
        }
    }
    private void OnSelect(int i)
    {
        selected_turret = turret_types[i];
        selected_pre = turret_types[i].pre_turret_type;
        upgradeUI.SetActive(false);
        cube_id = -1;
    }
}
